/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;

import entity.Client;
import entity.Commande;
import java.awt.Color;
import java.awt.EventQueue;
import java.beans.Beans;
import java.beans.PropertyVetoException;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.Normalizer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Parameter;
import javax.persistence.RollbackException;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.DesktopPaneUI;
import org.eclipse.persistence.internal.jpa.metamodel.CollectionAttributeImpl;
import org.eclipse.persistence.internal.jpa.parsing.TemporalLiteralNode;
import org.eclipse.persistence.jpa.jpql.parser.DateTime;

/**
 *
 * @author Khalid
 */
public class CommandeForm extends JPanel {

    JDesktopPane dskp = null;

    public CommandeForm(JDesktopPane jdsk) {
        dskp = jdsk;
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        loadData();
        Client cl = new Client();
        cl.setIdentite("--Aucun--");
        clientList.add(0, cl);
        jcmbClient.setSelectedIndex(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Commande c");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        clientQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Client c");
        clientList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clientQuery.getResultList();
        jPanel5 = new javax.swing.JPanel();
        totalLabel1 = new javax.swing.JLabel();
        sommePayeLabel1 = new javax.swing.JLabel();
        creditLabel1 = new javax.swing.JLabel();
        beneficeLabel1 = new javax.swing.JLabel();
        totalField1 = new javax.swing.JFormattedTextField();
        sommePayeField1 = new javax.swing.JFormattedTextField();
        creditField1 = new javax.swing.JFormattedTextField();
        beneficeField1 = new javax.swing.JFormattedTextField();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        clientIdLabel = new javax.swing.JLabel();
        refLabel = new javax.swing.JLabel();
        refField = new javax.swing.JTextField();
        dateCmdLabel = new javax.swing.JLabel();
        jspDate1 = new javax.swing.JSpinner();
        jcmbClient = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jspDate2 = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        produitListCellRenderer1 = new presentation.ProduitListCellRenderer();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Total: "));

        totalLabel1.setText("Total:");

        sommePayeLabel1.setText("Somme Paye:");

        creditLabel1.setText("Credit:");

        beneficeLabel1.setText("Benefice:");

        totalField1.setEditable(false);
        totalField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        totalField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        sommePayeField1.setEditable(false);
        sommePayeField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        sommePayeField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        creditField1.setEditable(false);
        creditField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        creditField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        beneficeField1.setEditable(false);
        beneficeField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        beneficeField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(totalLabel1)
                    .addComponent(sommePayeLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(totalField1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(sommePayeField1))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(creditLabel1)
                    .addComponent(beneficeLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(creditField1)
                    .addComponent(beneficeField1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(creditLabel1)
                            .addComponent(creditField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(beneficeLabel1)
                            .addComponent(beneficeField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(totalLabel1)
                            .addComponent(totalField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sommePayeLabel1)
                            .addComponent(sommePayeField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ref}"));
        columnBinding.setColumnName("Ref");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dateCmd}"));
        columnBinding.setColumnName("Date Cmd");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${clientId.identite}"));
        columnBinding.setColumnName("Client Id");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${total}"));
        columnBinding.setColumnName("Total");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sommePaye}"));
        columnBinding.setColumnName("Somme Paye");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${credit}"));
        columnBinding.setColumnName("Credit");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${benefice}"));
        columnBinding.setColumnName("Benefice");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtres"));

        clientIdLabel.setText("Client: ");

        refLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        refLabel.setText("Ref:");

        refField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        refField.setToolTipText("");

        dateCmdLabel.setText("Date  de création: ");

        jspDate1.setModel(new javax.swing.SpinnerDateModel());
        jspDate1.setEditor(new javax.swing.JSpinner.DateEditor(jspDate1, "dd/MM/yyyy"));

        jcmbClient.setToolTipText("");
        jcmbClient.setRenderer(produitListCellRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, clientList, jcmbClient);
        bindingGroup.addBinding(jComboBoxBinding);

        jButton1.setText("Actualiser");
        jButton1.addActionListener(formListener);

        jspDate2.setModel(new javax.swing.SpinnerDateModel());
        jspDate2.setEditor(new javax.swing.JSpinner.DateEditor(jspDate2, "dd/MM/yyyy"));

        jLabel1.setText(":");

        jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(refLabel)
                        .addGap(20, 20, 20)
                        .addComponent(refField, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clientIdLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcmbClient, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(dateCmdLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jspDate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jspDate2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(produitListCellRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(refLabel)
                            .addComponent(refField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(clientIdLabel)
                            .addComponent(jcmbClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(dateCmdLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jspDate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jspDate2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(produitListCellRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(76, 76, 76))
        );

        jButton2.setText("Ajouter");
        jButton2.addMouseListener(formListener);
        jButton2.addActionListener(formListener);

        jButton3.setText("Modifier");
        jButton3.addActionListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(masterScrollPane, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(13, 13, 13))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.MouseListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == jButton1) {
                CommandeForm.this.jButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton2) {
                CommandeForm.this.jButton2ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton3) {
                CommandeForm.this.jButton3ActionPerformed(evt);
            }
        }

        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (evt.getSource() == jButton2) {
                CommandeForm.this.jButton2MouseClicked(evt);
            }
        }

        public void mouseEntered(java.awt.event.MouseEvent evt) {
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
        }

        public void mousePressed(java.awt.event.MouseEvent evt) {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        loadData();


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:


    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        // JOptionPane.showMessageDialog(this, this.getParent());
        showiFrame(new CommandeDetailForm(null), "Ajouter commande");
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int index = masterTable.getSelectedRow();
        entity.Commande c = null;
        if (index != -1) {
            c = list.get(masterTable.convertRowIndexToModel(index));
        } else {
            JOptionPane.showMessageDialog(this, "Veuillez selecionner une ligne");
            return;
        }
        showiFrame(new CommandeDetailForm(c), "Modifier commande");
    }//GEN-LAST:event_jButton3ActionPerformed

    private void createWindow(Commande c) {

        //Create and set up the window. 
        JFrame frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

//       JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER); 
//       textLabel.setPreferredSize(new Dimension(300, 100)); 
        frame.getContentPane().add(new CommandeDetailForm(c));

        //Display the window. 
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    void showiFrame(JPanel frame, String title) {
        try {
            //JDesktopPane dskp = new JDesktopPane();
            //dskp.setSize(200, 200);
            JInternalFrame internalFrame = new JInternalFrame();
            internalFrame.setClosable(true);
            internalFrame.setMaximizable(true);
            internalFrame.setAutoscrolls(true);
            internalFrame.setIconifiable(true);
            internalFrame.setTitle(title);
            internalFrame.setSelected(true);
            internalFrame.setContentPane(frame);
            internalFrame.pack();
            internalFrame.setVisible(true);
            dskp.add(internalFrame);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(StartMDIApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void loadData() {
        //javax.persistence.Query queryCmd;
        String sqlTemp = "";
        String sqlCalTotal = "SELECT SUM(c.credit) AS credit,SUM(c.total) AS total,SUM(c.benefice) AS benefice,SUM(c.sommePaye) AS sommePaye FROM Commande c ";
        String sql = "SELECT c FROM Commande c ";
        Hashtable<String, Object> dic = new Hashtable<String, Object>();

        sqlTemp += "WHERE c.dateCmd BETWEEN :date_cmd1 AND :date_cmd2 ";

        if (!refField.getText().isEmpty()) {
            sqlTemp += "AND c.ref = :ref ";
            dic.put("ref", refField.getText());
        }
        if (jcmbClient.getSelectedIndex() != 0) {
            sqlTemp += "AND c.clientId = :client_id ";
            dic.put("client_id", ((entity.Client) jcmbClient.getSelectedItem()));
        }
        sql += sqlTemp;
        query = entityManager.createQuery(sql);
        query.setParameter("date_cmd1", new Date(((Date) jspDate1.getValue()).getTime()), TemporalType.DATE);
        query.setParameter("date_cmd2", new Date(((Date) jspDate2.getValue()).getTime()), TemporalType.DATE);

        sqlCalTotal += sqlTemp;
        javax.persistence.Query queryClaTotal = entityManager.createQuery(sqlCalTotal);
        queryClaTotal.setParameter("date_cmd1", new Date(((Date) jspDate1.getValue()).getTime()), TemporalType.DATE);
        queryClaTotal.setParameter("date_cmd2", new Date(((Date) jspDate2.getValue()).getTime()), TemporalType.DATE);

        Object[] results;

        for (Map.Entry<String, Object> entry : dic.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
            queryClaTotal.setParameter(entry.getKey(), entry.getValue());
        }

        results = (Object[]) queryClaTotal.getSingleResult();
        creditField1.setValue(results[0]);
        totalField1.setValue(results[1]);
        beneficeField1.setValue(results[2]);
        sommePayeField1.setValue(results[3]);

        java.util.Collection data = query.getResultList();
        list.clear();
        list.addAll(data);

    }

    String convertToString(Object o) {
        if (o != null) {
            return o.toString();
        } else {
            return "0";
        }
    }

    String getValueFormat(Object o) {
        return new SimpleDateFormat("dd/MM/yyyy").format(o);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField beneficeField1;
    private javax.swing.JLabel beneficeLabel1;
    private javax.swing.JLabel clientIdLabel;
    private java.util.List<entity.Client> clientList;
    private javax.persistence.Query clientQuery;
    private javax.swing.JFormattedTextField creditField1;
    private javax.swing.JLabel creditLabel1;
    private javax.swing.JLabel dateCmdLabel;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JComboBox jcmbClient;
    private javax.swing.JSpinner jspDate1;
    private javax.swing.JSpinner jspDate2;
    private java.util.List<entity.Commande> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private presentation.ProduitListCellRenderer produitListCellRenderer1;
    private javax.persistence.Query query;
    private javax.swing.JTextField refField;
    private javax.swing.JLabel refLabel;
    private javax.swing.JFormattedTextField sommePayeField1;
    private javax.swing.JLabel sommePayeLabel1;
    private javax.swing.JFormattedTextField totalField1;
    private javax.swing.JLabel totalLabel1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CommandeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CommandeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CommandeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CommandeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new CommandeForm(null));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}
